﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqData;
using Cricket.LinqModel;

public partial class ViewTeam : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Team team;
    List<Team> teams;
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //if (Session["AdminUser"] != null)
            //    SessionManager.CurrentUser = (LoggedInUser)Session["AdminUser"];
            HttpCookie cookie = Request.Cookies["Admin"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                bindTeam();
            }
        }
    }

    private void bindTeam()
    {
        team = new Team(_dataObject);
        List<GetAllTeamsResult> lst = new List<GetAllTeamsResult>();
        lst = team.GetAllTeams();
        if (lst.Count > 0)
        {
            rptTeam.DataSource = lst;
            rptTeam.DataBind();
        }
    }

    protected void rptTeam_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToString().ToLower().Trim() == "editteam")
        {
            System.Web.UI.WebControls.RepeaterItem row = (System.Web.UI.WebControls.RepeaterItem)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
            System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)row.FindControl("btnEdit");
            Response.Redirect(btnEdit.PostBackUrl);
        }
        if (e.CommandName.ToString().ToLower().Trim().Equals("deleteteam"))
        {
            System.Web.UI.WebControls.RepeaterItem row = (System.Web.UI.WebControls.RepeaterItem)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
            System.Web.UI.WebControls.Label hdnTeamId = (System.Web.UI.WebControls.Label)row.FindControl("hdnTeamId");
            team = new Team(_dataObject);
            team.Id = Convert.ToInt32(hdnTeamId.Text);
            team.DeleteTeam();
            divRed.InnerHtml = "<button data-dismiss='alert' class='close'></button><strong>Success! </strong>&nbsp;Record deleted successfully!";
            divRed.Attributes.Add("class", "alert alert-success");
            bindTeam();
        }
    }
    protected void rptTeam_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            System.Web.UI.WebControls.Label hdnTeamId = (System.Web.UI.WebControls.Label)e.Item.FindControl("hdnTeamId");
            System.Web.UI.WebControls.Label lblCommissionRate = (System.Web.UI.WebControls.Label)e.Item.FindControl("lblCommissionRate");
            System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)e.Item.FindControl("btnEdit");            
            btnEdit.PostBackUrl = "AddTeam.aspx?Id=" + hdnTeamId.Text;
        }
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddTeam.aspx");
    }
}